package org.reborn.utils

import scala.math
import org.reborn.math._

import TimeUtil._
import Util._


object Performance{
  
  def main(args:Array[String]){
    for_versus_loop();
    vectorFloatVersusVectorF();
  }
  
  def for_versus_loop() {
    
    //some non-trivial operation to test with
    def operation(input:Int):Int = {
      (input - input / 2) + input * 3
    }

    val warmup = 5
    val times = 20
    val operations = 500000

    var addWhile = TimeUtil.performance(times,warmup){()=>
        var i = 0
        while(i < operations){
          operation(i)
          i = i + 1
        }
    }
    println("while: " + TimeUtil.print(addWhile) + " ms")
    
    var addFor = TimeUtil.performance(times,warmup){()=>
        for(x <- 0 until operations){
          operation(x)
        }
    }
    println("for comprehension: " + TimeUtil.print(addFor) + " ms")
    
    
    
    var addLoop = TimeUtil.performance(times,warmup){()=>
        loop(0,_ < operations, 1){(x)=>
          operation(x)
        }
    }
    println("loop: " + TimeUtil.print(addLoop) + " ms")

    
    
    var addRLoop = performance(times,warmup){()=>
      rloop(0,_ < operations, 1){(x)=>
          operation(x)
        }
    }
    println("recursive loop: " + TimeUtil.print(addRLoop) + " ms")
    
    var addFULoop = performance(times,warmup){()=>
      loop(0, operations){(x)=>
          operation(x)
        }
    }
    println("for until loop: " + TimeUtil.print(addFULoop) + " ms")
  }

  def vectorFloatVersusVectorF() {
    
    def randomList():List[Float] ={
      List(math.random.asInstanceOf[Float],math.random.asInstanceOf[Float],math.random.asInstanceOf[Float])
    }
    
    val warmup = 10
    val times = 40
    val operations = 50000

    var base = TimeUtil.performance(times,warmup){()=>
      var i = 0
      while(i < operations){
        randomList
        randomList
        i += 1
      }
    }
    println("base: " + TimeUtil.print(base) + " ms")
    
    var vectorF = TimeUtil.performance(times,warmup){()=>
      var i = 0
      while(i < operations){
        VectorF(randomList) dot VectorF(randomList)
        i += 1
      }
    }
    println("VectorF: " + TimeUtil.print(vectorF) + " ms")

  }
}
